﻿Imports System.Data
Imports System.Data.Odbc
Imports System.Text
Imports System.Collections.Generic
Imports WSEntidades
Imports WSFactory

Public Class AlmVendedorDAL
    Public Function registraAlmacenVend(ByVal pAlmacenVend As AlmVendedorBE) As Integer
        Dim instrSql As String
        Dim oInsParm(0 To 3) As OdbcParameter
        Try

            instrSql = "INSERT INTO ""AlmVendedor""(""ClaveMat"", ""Existencia"", ""IdEmpresa"", ""IdVendedor"" " & _
                                        "VALUES (?,?,?,?);"

            oInsParm(0) = New OdbcParameter("", pAlmacenVend.ClaveMat)
            oInsParm(1) = New OdbcParameter("", pAlmacenVend.Existencia)
            oInsParm(2) = New OdbcParameter("", pAlmacenVend.IdEmpresa)
            oInsParm(3) = New OdbcParameter("", pAlmacenVend.IdVendedor)

            'Crea el objeto connection
            Using myConn As OdbcConnection = New OdbcConnection(My.Settings.cs)
                Using myComm As New OdbcCommand(instrSql, myConn)
                    myComm.CommandType = CommandType.Text
                    myComm.Parameters.AddRange(oInsParm)
                    myConn.Open()
                    myComm.ExecuteNonQuery()
                    If (myComm.ExecuteNonQuery() > 0) Then
                        Return True
                    Else
                        Return False
                    End If
                End Using
            End Using

           
        Catch ex As Exception
            Throw
        Finally
            oInsParm = Nothing
            instrSql = Nothing
        End Try
    End Function

    Public Function modificaAlmacenVend(ByVal pAlmacenVend As AlmVendedorBE) As Boolean
        Dim instrsql As String
        Dim oUpdParm(0 To 2) As OdbcParameter
        Try
            instrsql = "UPDATE ""AlmVendedor"" " & _
                                        "SET ""Existencia"" = ? " & _
                                            "WHERE ""IdVendedor"" = ? " & _
                                            "AND ""IdEmpresa"" = ?"

            oUpdParm(0) = New OdbcParameter("", pAlmacenVend.Existencia)
            oUpdParm(1) = New OdbcParameter("", pAlmacenVend.IdVendedor)
            oUpdParm(2) = New OdbcParameter("", pAlmacenVend.IdEmpresa)
            'Crea el objeto connection
            Using myConn As OdbcConnection = New OdbcConnection(My.Settings.cs)
                Using myComm As New OdbcCommand(instrsql, myConn)
                    myComm.CommandType = CommandType.Text
                    myComm.Parameters.AddRange(oUpdParm)
                    myConn.Open()
                    myComm.ExecuteNonQuery()
                    If (myComm.ExecuteNonQuery() > 0) Then
                        Return True
                    Else
                        Return False
                    End If
                End Using
            End Using
            
        Catch ex As Exception
            Throw
        Finally
            oUpdParm = Nothing
            instrsql = Nothing
        End Try
    End Function

    Public Function getAlmacenesVend(ByVal pIdEmpr As Integer, ByVal pIdVendedor As Integer) As List(Of AlmVendedorBE)
        Dim instrSql As String
        Dim almvendLs As List(Of AlmVendedorBE) = New List(Of AlmVendedorBE)
        Try
            instrSql = "SELECT ""IdEmpresa"", ""IdVendedor"", ""Existencia"", ""ClaveMat"" " & _
                                     "FROM ""AlmVendedor"" " & _
                                    "WHERE ""IdEmpresa"" = ? " & _
                                        "AND ""IdVendedor"" = ? ;"

            Using myConn As OdbcConnection = New OdbcConnection(My.Settings.cs)
                Using myComm As OdbcCommand = New OdbcCommand(instrSql, myConn)
                    myComm.CommandType = CommandType.Text
                    myComm.Parameters.Add(New OdbcParameter("", pIdEmpr))
                    myComm.Parameters.Add(New OdbcParameter("", pIdVendedor))
                    myConn.Open()
                    Using myDR As OdbcDataReader = myComm.ExecuteReader()
                        While myDR.Read()
                            Dim almvendedores As AlmVendedorBE = almvendedorFact.Build(myDR)
                            If (Not almvendedores Is Nothing) Then
                                almvendLs.Add(almvendedores)
                            End If
                            almvendedores = Nothing
                        End While
                    End Using
                End Using
            End Using
            Return almvendLs
            
        Catch ex As Exception
            Throw 
        Finally
            almvendLs = Nothing
            instrSql = Nothing
        End Try
    End Function
End Class
